package cn.wuhan2019mwg.mobile.activity.wuhan2019mwg.activitys;

import android.app.AlertDialog;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.SharedPreferences;
import android.content.res.Configuration;
import android.content.res.Resources;
import android.location.Location;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.support.v7.widget.AppCompatTextView;
import android.text.TextUtils;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.View;
import android.view.WindowManager;
import android.webkit.WebChromeClient;
import android.webkit.WebResourceError;
import android.webkit.WebResourceRequest;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.blankj.utilcode.util.SizeUtils;
import com.loopj.android.http.AsyncHttpClient;
import com.loopj.android.http.AsyncHttpResponseHandler;
import com.loopj.android.http.PersistentCookieStore;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;
import com.shelwee.update.UpdateHelper;
import com.zyyoona7.lib.EasyPopup;
import com.zyyoona7.lib.HorizontalGravity;
import com.zyyoona7.lib.VerticalGravity;

import org.apache.http.cookie.Cookie;
import org.apache.http.entity.*;

import org.json.JSONObject;
import org.w3c.dom.Text;

import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;

import cn.sharesdk.framework.Platform;
import cn.sharesdk.framework.PlatformActionListener;
import cn.sharesdk.framework.PlatformDb;
import cn.sharesdk.framework.ShareSDK;
import cn.sharesdk.framework.Platform.ShareParams;
import cn.sharesdk.wechat.friends.Wechat;
import cn.sharesdk.wechat.moments.WechatMoments;
import cn.wuhan2019mwg.mobile.activity.wuhan2019mwg.R;
import cn.wuhan2019mwg.mobile.activity.wuhan2019mwg.adapter.WZAdapter;
import cn.wuhan2019mwg.mobile.activity.wuhan2019mwg.base.BaseActivity;
import cn.wuhan2019mwg.mobile.activity.wuhan2019mwg.mwgapplication.WuhanMwgApplication;
import cn.wuhan2019mwg.mobile.activity.wuhan2019mwg.utils.Constance;

import cn.wuhan2019mwg.mobile.activity.wuhan2019mwg.utils.ToastUtils;
import de.hdodenhof.circleimageview.CircleImageView;

public class MainActivity extends BaseActivity implements View.OnClickListener ,PlatformActionListener {

//    private FloatingActionButton fab;
    private String tag="MainActivity";
    private WebView web_main;
    private RelativeLayout main_title , rl_more,main_bottom,rl_1,rl_2,rl_3,rl_4,rl5,rl_5;
    private String current_url="";
    private ProgressBar pb_progress;
    private EasyPopup mQQPop;
    private TextView tv_chinese,tv_english,tv_france,tvlogin,tvck,tvtlogin;
    private ImageView iv_1,iv_2,iv_3,iv_4,iv_5;
    private AppCompatTextView actv_1,actv_2,actv_3,actv_4,actv_5;
    LocaleChangeReceiver mLocaleChangeReceiver = new LocaleChangeReceiver();
    private String str1="";
    private String str2="";
    private String str3="";
    private String str4="";
    private String str5="";
    private int index=0;
    private ImageView iv_ch,iv_en,iv_fr,wx;
//    private CircleImageView ivu;
    private WZAdapter wzAdapter;
    private ListView hyview;
    private Platform wechat = null;
    private Platform qqchat = null;
//    private Platform mPlatform;
    private static final int MSG_AUTH_CANCEL = 5;
    private static final int MSG_AUTH_ERROR = 6;
    private static final int MSG_AUTH_COMPLETE = 7;
    protected AsyncHttpClient asyncHttpClient;

    /** 微信好友注册数据 */
    private HashMap<String, Object> map_wxFriend;


    public static final String    APPKEY ="21d37599e44a0";
    public static final String    APPID_WXFRIEND = "wxd707fe1df5f23804";
    public static final String    APPSECRET_WXFRIEND = "b57b61bb297d1c44c831dc3c038afee7";
    public static final String    BYPASSAPPROVAL_WXFRIEND = "false";
    public static final String    ENABLE_WXFRIEND = "true";


    public class LocaleChangeReceiver extends BroadcastReceiver {




        @Override
        public void onReceive(Context context, Intent intent) {
            Log.v(tag, "mReceiver  onReceive  intent.getAction(): "+intent.getAction());

            if(intent.getAction().equals(Intent.ACTION_LOCALE_CHANGED)) {
                Log.e("tag","Language change "   );
                Locale locale = Locale.getDefault();
                String language = locale.getLanguage();
                if(language.equals("zh"))
                {
                    str1 = Constance.MAIN_URL;
                    str2 = Constance.ACTIVITY_URL;
                    str3 = Constance.COP_HOME;
                    str4 = Constance.SET_HOME;
                    str5 = Constance.ACTIVITY_URL1;
                }
                else if(language.equals("en"))
                {
                    str1 = Constance.MAIN_URL_E;
                    str2 = Constance.ACTIVITY_URL_E;
                    str3 = Constance.COP_HOME_E;
                    str4 = Constance.SET_HOME_E;
                }
                else if(language.equals("fr"))
                {
                    str1 = Constance.MAIN_URL_F;
                    str2 = Constance.ACTIVITY_URL_F;
                    str3 = Constance.COP_HOME_F;
                    str4 = Constance.SET_HOME_F;
                }
                switch (index)
                {
                    case 0:
                        web_main.loadUrl(str1);
                        break;
                    case 1:
                        web_main.loadUrl(str2);
                        break;
                    case 2:
                        web_main.loadUrl(str3);
                        break;
                    case 3:
                        web_main.loadUrl(str4);
                        break;
                    case 4:
                        web_main.loadUrl(str5);
                        break;

                }


            }
        }
    }





    @Override
    protected int setLayoutId() {
        return R.layout.activity_main;
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        UpdateHelper updateHelper = new UpdateHelper.Builder(this)
                .checkUrl("http://www.wuhan2019mwg.cn/appupdate.json")
                .isAutoInstall(false) //设置为false需在下载完手动点击安装;默认值为true，下载后自动安装。
                .build();
        updateHelper.check();

    }



    @Override
    protected void initView() {
//        fab = (FloatingActionButton) findViewById(R.id.fab);
//        fab.setBackgroundResource(R.mipmap.home);
        wx   = (ImageView) findViewById(R.id.ivtl);
        wx.setVisibility(View.GONE);
        main_title = (RelativeLayout)findViewById(R.id.main_title);
        rl_more = (RelativeLayout) main_title.findViewById(R.id.rl_more);
        pb_progress = (ProgressBar)main_title.findViewById(R.id.pb_progress);
        web_main = (WebView)findViewById(R.id.web_main);
        main_bottom = (RelativeLayout) findViewById(R.id.main_bottom);
        tvtlogin = (TextView) findViewById(R.id.tvtlogin);
        tvtlogin.setVisibility(View.GONE);
        rl_1 = (RelativeLayout) main_bottom.findViewById(R.id.rl_1);
        rl_2 = (RelativeLayout) main_bottom.findViewById(R.id.rl_2);
        rl_3 = (RelativeLayout) main_bottom.findViewById(R.id.rl_3);
        rl_4 = (RelativeLayout) main_bottom.findViewById(R.id.rl_4);
        rl_5 = (RelativeLayout) main_bottom.findViewById(R.id.rl_5);
        rl5 = (RelativeLayout) findViewById(R.id.rl5);
        rl5.setVisibility(View.INVISIBLE);
        iv_1 = (ImageView) main_bottom.findViewById(R.id.iv_1);
        iv_2 = (ImageView) main_bottom.findViewById(R.id.iv_2);
        iv_3 = (ImageView) main_bottom.findViewById(R.id.iv_3);
        iv_4 = (ImageView) main_bottom.findViewById(R.id.iv_4);
        iv_5 = (ImageView) main_bottom.findViewById(R.id.iv_5);
        actv_1 = (AppCompatTextView) main_bottom.findViewById(R.id.actv_1);
        actv_2 = (AppCompatTextView) main_bottom.findViewById(R.id.actv_2);
        actv_3 = (AppCompatTextView) main_bottom.findViewById(R.id.actv_3);
        actv_4 = (AppCompatTextView) main_bottom.findViewById(R.id.actv_4);
        actv_5 = (AppCompatTextView) main_bottom.findViewById(R.id.actv_5);
        tvlogin = (TextView) findViewById(R.id.tvlogin);
        tvlogin.setVisibility(View.GONE);
        tvck = (TextView) findViewById(R.id.tvck);
        hyview = (ListView) findViewById(R.id.hyview);
        hyview.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener(){
            @Override
            public boolean onItemLongClick(AdapterView<?> arg0, View arg1,
                                           int arg2, long arg3) {
                // TODO Auto-generated method stub
                DeleteData(arg2);
                return true;
            }
        });


        hyview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Intent intent = new Intent();
                intent.setClass(MainActivity.this,OtherWebActivity.class);
                intent.putExtra(Constance.URL,WuhanMwgApplication.anAbstract.getSublist().get(position).getUrl());
                startActivity(intent);
            }
        });


        iv_ch = (ImageView) findViewById(R.id.iv_ch);
        iv_en = (ImageView) findViewById(R.id.iv_en);
        iv_fr = (ImageView) findViewById(R.id.iv_fr);
 //       ivu = (CircleImageView) findViewById(R.id.ivu);
 //       ivu.setOnClickListener(this);
        iv_ch.setOnClickListener(this);
        iv_en.setOnClickListener(this);
        iv_fr.setOnClickListener(this);
        rl_1.setOnClickListener(this);
        rl_2.setOnClickListener(this);
        rl_3.setOnClickListener(this);
        rl_4.setOnClickListener(this);
        rl_5.setOnClickListener(this);
        rl_more.setOnClickListener(this);
        wx.setOnClickListener(this);
//        rl_more.setVisibility(View.INVISIBLE);
//        fab.setOnClickListener(this);

    }

    /**
     * 初始化各个平台注册信息
     */
    private void initRegistInfo() {

        map_wxFriend = new HashMap<String, Object>();
        map_wxFriend.put("AppId", APPID_WXFRIEND);
        map_wxFriend.put("AppSecret",APPSECRET_WXFRIEND);
        map_wxFriend.put("Enable", ENABLE_WXFRIEND);
        map_wxFriend.put("BypassApproval", BYPASSAPPROVAL_WXFRIEND);
        map_wxFriend.put("ShortLinkConversationEnable", "true");
        ShareSDK.setPlatformDevInfo(Wechat.NAME, map_wxFriend);


    }


    private void onThirdLogin(){

         wechat = ShareSDK.getPlatform(this, Wechat.NAME);
         wechat.setPlatformActionListener(this);
         wechat.SSOSetting(false);
         wechat.showUser(null);
    }

    @Override
    protected void initData() {


        ShareSDK.initSDK(this,APPKEY);
        initRegistInfo();

        if (asyncHttpClient == null) {
            asyncHttpClient = new AsyncHttpClient();
            asyncHttpClient.setTimeout(20000);
        }
        setWebClient();
        initQQPop();
        IntentFilter intentFilter =new IntentFilter();
        intentFilter.addAction(Intent.ACTION_LOCALE_CHANGED);
        //registerReceiver(mLocaleChangeReceiver,intentFilter);
//        Locale locale = Locale.getDefault();
//        String language = locale.getLanguage();
        String language = WuhanMwgApplication.sSharedPreferences.getString(Constance.language,"zh");
        if(language.equals("zh"))
        {
            str1 = Constance.MAIN_URL;
            str2 = Constance.ACTIVITY_URL;
            str3 = Constance.COP_HOME;
            str4 = Constance.SET_HOME;
            iv_ch.setImageResource(R.mipmap.ch1);
            iv_en.setImageResource(R.mipmap.en);
            iv_fr.setImageResource(R.mipmap.fr);
            rl_5.setVisibility(View.VISIBLE);
        }
        else if(language.equals("en"))
        {
            str1 = Constance.MAIN_URL_E;
            str2 = Constance.ACTIVITY_URL_E;
            str3 = Constance.COP_HOME_E;
            str4 = Constance.SET_HOME_E;
            iv_ch.setImageResource(R.mipmap.ch);
            iv_en.setImageResource(R.mipmap.en1);
            iv_fr.setImageResource(R.mipmap.fr);
            rl_5.setVisibility(View.GONE);
        }
        else if(language.equals("fr"))
        {
            str1 = Constance.MAIN_URL_F;
            str2 = Constance.ACTIVITY_URL_F;
            str3 = Constance.COP_HOME_F;
            str4 = Constance.SET_HOME_F;
            iv_ch.setImageResource(R.mipmap.ch);
            iv_en.setImageResource(R.mipmap.en);
            iv_fr.setImageResource(R.mipmap.fr1);
            rl_5.setVisibility(View.GONE);
        }
        setTabState(rl_1);

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        //unregisterReceiver(mLocaleChangeReceiver);
    }

    @Override
    protected void onResume() {
        super.onResume();
        if(WuhanMwgApplication.anAbstract!=null)
        {
            //Log.e(tag,"WuhanMwgApplication.anAbstract!=null");
            wx.setVisibility(View.GONE);
            tvtlogin.setVisibility(View.GONE);
            mImageLoader = ImageLoader.getInstance();
  //          ImageLoader.getInstance().init(ImageLoaderConfiguration.createDefault(this));
 //           mImageLoader.displayImage(WuhanMwgApplication.anAbstract.getUcenter().get(0).getAvatar(), ivu);
            tvlogin.setText(WuhanMwgApplication.anAbstract.getUcenter().get(0).getNickname());
            wzAdapter = new WZAdapter(this,WuhanMwgApplication.anAbstract.getSublist());
            hyview.setAdapter(wzAdapter);
            if(WuhanMwgApplication.sSharedPreferences.getString(Constance.language,"zh").equals("zh"))
            {
                tvck.setText(WuhanMwgApplication.anAbstract.getLanguage().get(0).getSubs());
            }
            else if(WuhanMwgApplication.sSharedPreferences.getString(Constance.language,"zh").equals("en"))
            {
                tvck.setText(WuhanMwgApplication.anAbstract.getLanguage().get(1).getSubs());
            }
            else if(WuhanMwgApplication.sSharedPreferences.getString(Constance.language,"zh").equals("fr"))
            {
                tvck.setText(WuhanMwgApplication.anAbstract.getLanguage().get(2).getSubs());
            }
        }
        else
        {
            wx.setVisibility(View.GONE);
 //           ivu.setImageResource(R.mipmap.pic_user_image);
            hyview.setAdapter(null);
            tvck.setText(getString(R.string.ckwz));
            tvlogin.setText(getString(R.string.login));
            tvtlogin.setVisibility(View.GONE);
        }

    }

    private void initQQPop() {
        mQQPop = new EasyPopup(MainActivity.this)
                .setContentView(R.layout.layout_top_righy_pop)
                .setAnimationStyle(R.style.QQPopAnim)
                .setFocusAndOutsideEnable(true)
                .createPopup();
        tv_chinese = mQQPop.getView(R.id.tv_chinese);
        tv_english = mQQPop.getView(R.id.tv_english);
        tv_france = mQQPop.getView(R.id.tv_france);

        tv_chinese.setOnClickListener(MainActivity.this);
        tv_english.setOnClickListener(MainActivity.this);
        tv_france.setOnClickListener(MainActivity.this);
    }

    private void showQQPop(View view) {
        mQQPop.showAtAnchorView(view, VerticalGravity.BELOW, HorizontalGravity.RIGHT, SizeUtils.dp2px(30), 0);
    }


    private void setWebClient(){
        web_main.getSettings().setCacheMode(WebSettings.LOAD_NO_CACHE);

        web_main.getSettings().setJavaScriptEnabled(true);// 设置可用JS
        web_main.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
        web_main.getSettings().setUseWideViewPort(true);
        web_main.getSettings().setLoadWithOverviewMode(true);
//		wb_other.getSettings().setBlockNetworkImage(true);
        web_main.getSettings().setSaveFormData(true);
        web_main.getSettings().setAllowFileAccess(true);
        web_main.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
        web_main.getSettings().setGeolocationEnabled(true);
        web_main.getSettings().setDomStorageEnabled(true);



        WindowManager wm = (WindowManager) MainActivity.this.getSystemService(Context.WINDOW_SERVICE);
        int width = wm.getDefaultDisplay().getWidth();
        if(width > 650)
        {
            this.web_main.setInitialScale(190);
        }else if(width > 520)
        {
            this.web_main.setInitialScale(160);
        }else if(width > 450)
        {
            this.web_main.setInitialScale(140);
        }else if(width > 300)
        {
            this.web_main.setInitialScale(120);
        }else
        {
            this.web_main.setInitialScale(100);
        }

        web_main.setWebViewClient(new WebViewClient(){
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {

                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {

                    if (request.getUrl().toString().contains("/dt")){
                        Intent intent = new Intent();
                        intent.putExtra(Constance.URL,request.getUrl().toString());
                        intent.setClass(MainActivity.this, OtherWebActivity.class);
                        startActivity(intent);
                    }else {
                        view.loadUrl(request.getUrl().toString());
                        current_url = request.getUrl().toString();

                    }

                }


                    Log.e("strurl","WebResourceRequest"+current_url);


                return true;
            }

            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {

                if (url.contains("/dt") || url.contains("/Latest_news")){
                    Intent intent = new Intent();
                    intent.putExtra(Constance.URL,url);
                    intent.setClass(MainActivity.this, OtherWebActivity.class);
                    startActivity(intent);
                }else {
                    view.loadUrl(url);
                    current_url = url;
                    Log.e("strurl", "String url " + url);
                }
                return true;
            }

            @Override
            public void onReceivedError(WebView view, WebResourceRequest request, WebResourceError error) {
                super.onReceivedError(view, request, error);
            }
        });

        web_main.setWebChromeClient(new WebChromeClient(){

            @Override
            public void onProgressChanged(WebView view, int newProgress) {
                if (newProgress == 100) {
                    // 网页加载完成
                    pb_progress.setVisibility(View.GONE);
                } else {
                    // 加载中
                    pb_progress.setVisibility(View.VISIBLE);
                    pb_progress.setProgress(newProgress);
                }
                Log.e("progress", "values-zh-rCN " +newProgress);
                super.onProgressChanged(view, newProgress);
            }

            @Override
            public void onReceivedTitle(WebView view, String title) {
                super.onReceivedTitle(view, title);

                Log.e("strurl",title);
            }
        });


    }

  private void setTabState(RelativeLayout rl){
      if (rl.getId() == R.id.rl_1) {
          iv_1.setEnabled(true);
          iv_2.setEnabled(false);
          iv_3.setEnabled(false);
          iv_4.setEnabled(false);
          iv_5.setEnabled(false);
          actv_5.setTextColor(getResources().getColor(R.color.tabTextColor));
          actv_1.setTextColor(getResources().getColor(R.color.mainColor));
          actv_2.setTextColor(getResources().getColor(R.color.tabTextColor));
          actv_3.setTextColor(getResources().getColor(R.color.tabTextColor));
          actv_4.setTextColor(getResources().getColor(R.color.tabTextColor));
          index=0;
          web_main.loadUrl(str1);
          rl5.setVisibility(View.INVISIBLE);
          web_main.setVisibility(View.VISIBLE);
      } else if (rl.getId() == R.id.rl_2) {
          iv_1.setEnabled(false);
          iv_2.setEnabled(true);
          iv_3.setEnabled(false);
          iv_4.setEnabled(false);
          actv_1.setTextColor(getResources().getColor(R.color.tabTextColor));
          actv_2.setTextColor(getResources().getColor(R.color.mainColor));
          actv_3.setTextColor(getResources().getColor(R.color.tabTextColor));
          actv_4.setTextColor(getResources().getColor(R.color.tabTextColor));
          iv_5.setEnabled(false);
          actv_5.setTextColor(getResources().getColor(R.color.tabTextColor));
          index=1;
          web_main.loadUrl(str2);
          rl5.setVisibility(View.INVISIBLE);
          web_main.setVisibility(View.VISIBLE);
      } else if (rl.getId() == R.id.rl_3) {
          iv_1.setEnabled(false);
          iv_2.setEnabled(false);
          iv_3.setEnabled(true);
          iv_4.setEnabled(false);
          actv_1.setTextColor(getResources().getColor(R.color.tabTextColor));
          actv_2.setTextColor(getResources().getColor(R.color.tabTextColor));
          actv_3.setTextColor(getResources().getColor(R.color.mainColor));
          actv_4.setTextColor(getResources().getColor(R.color.tabTextColor));
          iv_5.setEnabled(false);
          actv_5.setTextColor(getResources().getColor(R.color.tabTextColor));
          index=2;
          web_main.loadUrl(str3);
          rl5.setVisibility(View.INVISIBLE);
          web_main.setVisibility(View.VISIBLE);
      }else if (rl.getId() == R.id.rl_4){
          iv_1.setEnabled(false);
          iv_2.setEnabled(false);
          iv_3.setEnabled(false);
          iv_4.setEnabled(true);
          actv_1.setTextColor(getResources().getColor(R.color.tabTextColor));
          actv_2.setTextColor(getResources().getColor(R.color.tabTextColor));
          actv_3.setTextColor(getResources().getColor(R.color.tabTextColor));
          actv_4.setTextColor(getResources().getColor(R.color.mainColor));
          index=3;
          rl5.setVisibility(View.VISIBLE);
          web_main.setVisibility(View.INVISIBLE);
          iv_5.setEnabled(false);
          actv_5.setTextColor(getResources().getColor(R.color.tabTextColor));
      }
      else if (rl.getId() == R.id.rl_5){
          iv_1.setEnabled(false);
          iv_2.setEnabled(false);
          iv_3.setEnabled(false);
          iv_4.setEnabled(false);
          actv_1.setTextColor(getResources().getColor(R.color.tabTextColor));
          actv_2.setTextColor(getResources().getColor(R.color.tabTextColor));
          actv_3.setTextColor(getResources().getColor(R.color.tabTextColor));
          actv_4.setTextColor(getResources().getColor(R.color.tabTextColor));
          index=4;
          web_main.loadUrl(str1);
          rl5.setVisibility(View.INVISIBLE);
          web_main.setVisibility(View.VISIBLE);
          iv_5.setEnabled(true);
          actv_5.setTextColor(getResources().getColor(R.color.mainColor));
      }
  }

    @Override
    public void onBackPressed() {
        if (web_main.canGoBack()){
             web_main.goBack();
        }else {

            AlertDialog.Builder dialog = new AlertDialog.Builder(this);
            dialog.setIcon(android.R.drawable.ic_dialog_info);
            dialog.setTitle(getResources().getString(R.string.prompt));
            dialog.setIcon(R.mipmap.app_icon);
            dialog.setMessage(getResources().getString(R.string.exitp));
            dialog.setPositiveButton(getResources().getString(R.string.sure),
                    new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            finish();
                        }
                    });
            dialog.setNegativeButton(getResources().getString(R.string.cancel),
                    new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            dialog.dismiss();
                        }
                    });
            dialog.show();
        }
    }

    public void Logout()
    {
        AlertDialog.Builder dialog = new AlertDialog.Builder(this);
        dialog.setIcon(android.R.drawable.ic_dialog_info);
        dialog.setTitle(getResources().getString(R.string.prompt));
        dialog.setIcon(R.mipmap.app_icon);
        dialog.setMessage(getResources().getString(R.string.exitl));
        dialog.setPositiveButton(getResources().getString(R.string.sure),
                new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        Intent intent = new Intent();
                        intent.putExtra(Constance.URL,"http://uc.wuhan2019mwg.cn/index.php?m=member&c=index&a=logout&forward=&siteid=");
                        intent.setClass(getApplicationContext(),OtherWebActivity.class);
                        startActivity(intent);
                        WuhanMwgApplication.anAbstract=null;
                    }
                });
        dialog.setNegativeButton(getResources().getString(R.string.cancel),
                new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.dismiss();
                    }
                });
        dialog.show();
    }

    public void DeleteData(int a)
    {
        final int b =a;
        AlertDialog.Builder dialog = new AlertDialog.Builder(this);
        dialog.setIcon(android.R.drawable.ic_dialog_info);
        dialog.setTitle(getResources().getString(R.string.prompt));
        dialog.setIcon(R.mipmap.app_icon);
        dialog.setMessage(getResources().getString(R.string.delete));
        dialog.setPositiveButton(getResources().getString(R.string.sure),
                new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        Intent intent = new Intent();
                        intent.putExtra(Constance.URL,WuhanMwgApplication.anAbstract.getSublist().get(b).getDelete());
                        intent.putExtra(Constance.TYPE,1);
                        intent.setClass(getApplicationContext(),OtherWebActivity.class);
                        startActivity(intent);
                    }
                });
        dialog.setNegativeButton(getResources().getString(R.string.cancel),
                new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.dismiss();
                    }
                });
        dialog.show();
    }

    @Override
    public void onClick(View v) {
         switch (v.getId()){

//             case R.id.fab:
//                 ToastUtils.showSafeToast(MainActivity.this,"Fab");
//                 break;

             case R.id.rl_more:

                 showQQPop(v);
                 break;

             case R.id.tv_chinese:
//                 web_main.loadUrl(Constance.CN_HOME);
                 handler.sendEmptyMessage(1);
                 mQQPop.dismiss();
                 break;

             case R.id.tv_english:
//                 web_main.loadUrl(Constance.EN_HOME);
                 handler.sendEmptyMessage(2);
                 mQQPop.dismiss();
                 break;

             case R.id.tv_france:
//                 web_main.loadUrl(Constance.FR_HOME);
                 handler.sendEmptyMessage(3);
                 mQQPop.dismiss();
                 break;

             case R.id.rl_1:
                 setTabState(rl_1);
                 break;

             case R.id.rl_2:
                 setTabState(rl_2);
                 break;

             case R.id.rl_3:
                 setTabState(rl_3);
                 break;

             case R.id.rl_4:
                 setTabState(rl_4);
                 if(WuhanMwgApplication.anAbstract!=null)
                 {
                     Intent intent = new Intent();
                     intent.putExtra(Constance.URL,"http://uc.wuhan2019mwg.cn/index.php?m=member");
                     intent.setClass(getApplicationContext(),OtherWebActivity.class);
                     startActivity(intent);
                 }
                 break;

             case R.id.rl_5:
                 setTabState(rl_5);
                 break;


             case R.id.iv_ch:
                 handler.sendEmptyMessage(1);
                 break;

             case R.id.iv_en:
                 handler.sendEmptyMessage(2);
                 break;

             case R.id.iv_fr:
                 handler.sendEmptyMessage(3);
                 break;
             case R.id.ivtl:
//                 wechat = ShareSDK.getPlatform(this, Wechat.NAME);
//                 wechat.setPlatformActionListener(this);
//                 wechat.SSOSetting(false);
//                 wechat.showUser(null);
//                 onThirdLogin();
                 break;
//             case R.id.ivu:
//                 if(WuhanMwgApplication.anAbstract==null)
//                 {
//                     Intent intent = new Intent();
//                     intent.putExtra(Constance.URL,"http://uc.wuhan2019mwg.cn/index.php?m=member&c=index&a=login");
//                     intent.setClass(getApplicationContext(),OtherWebActivity.class);
//                     startActivity(intent);
//                 }
//                 else
//                 {
//                     Logout();
//                 }
//                 break;
         }
    }

    private Handler handler = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what){
                case 1:
                    Resources resources = getApplicationContext().getResources();
                     DisplayMetrics dm = resources.getDisplayMetrics();
                     Configuration config = resources.getConfiguration();
                    Locale locale = getApplicationContext().getResources().getConfiguration().locale;
                     locale = Locale.CHINA;
                    config.setLocale(locale);
                     resources.updateConfiguration(config, dm);
                    WuhanMwgApplication.sEditor.putString(Constance.language,"zh");
                    WuhanMwgApplication.sEditor.commit();
                    finish();
                    Intent intent = new Intent(getApplicationContext(), MainActivity.class);
                    startActivity(intent);
                    break;
                case 2:
                    Resources resources1 = getApplicationContext().getResources();
                    DisplayMetrics dm1 = resources1.getDisplayMetrics();
                    Configuration config1 = resources1.getConfiguration();
                    Locale locale1 = getApplicationContext().getResources().getConfiguration().locale;
                    locale1 = Locale.ENGLISH;
                    config1.setLocale(locale1);
                    resources1.updateConfiguration(config1, dm1);
                    WuhanMwgApplication.sEditor.putString(Constance.language,"en");
                    WuhanMwgApplication.sEditor.commit();
                    finish();
                    Intent intent1 = new Intent(getApplicationContext(), MainActivity.class);
                    startActivity(intent1);
                    break;
                case 3:
                    Resources resources2 = getApplicationContext().getResources();
                    DisplayMetrics dm2 = resources2.getDisplayMetrics();
                    Configuration config2 = resources2.getConfiguration();
                    Locale locale2 = getApplicationContext().getResources().getConfiguration().locale;
                    locale2 = Locale.FRANCE;
                    config2.setLocale(locale2);
                    resources2.updateConfiguration(config2, dm2);
                    finish();
                    WuhanMwgApplication.sEditor.putString(Constance.language,"fr");
                    WuhanMwgApplication.sEditor.commit();
                    Intent intent2 = new Intent(getApplicationContext(), MainActivity.class);
                    startActivity(intent2);
                    break;
                case MSG_AUTH_COMPLETE:
                    Log.e(tag, "MSG_AUTH_COMPLETE ");
//                    if (wechat != null) {
//                        wechat.removeAccount();
//                    } else {
//                        qqchat.removeAccount();
//                    }
                    userLogin();

                    break;
                case MSG_AUTH_ERROR:
                    Log.e(tag, "MSG_AUTH_ERROR ");
                    break;
                case MSG_AUTH_CANCEL:
                    Log.e(tag, "MSG_AUTH_CANCEL ");


                default:
                    break;
            }
        }
    };



    public void onComplete(Platform platform, int action, HashMap<String, Object> res) {
        if (action == Platform.ACTION_USER_INFOR) {

            PlatformDb platDB = platform.getDb();//获取数平台数据DB
            platDB.getUserIcon();
            platDB.getUserName();

            Log.e("PLATFORMDB","USERIMAGE == "+ platDB.getUserIcon() + "\n USERNAME == "+platDB.getUserName());

            Message msg = new Message();

            msg.what = MSG_AUTH_COMPLETE;
            wechat = platform;
            Log.e(tag, "onComplete");
            handler.sendMessage(msg);



        }
    }


    public void onError(Platform platform, int action, Throwable t) {
        Log.e(tag, "onError");
        if (action == Platform.ACTION_USER_INFOR) {
            handler.sendEmptyMessage(MSG_AUTH_ERROR);
        }
        t.printStackTrace();
    }

    public void onCancel(Platform platform, int action) {
        Log.e(tag, "onCancel");
        if (action == Platform.ACTION_USER_INFOR) {
            handler.sendEmptyMessage(MSG_AUTH_CANCEL);
        }
    }


    protected void userLogin() {
        try {
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("uid", wechat.getDb().getUserId());
            jsonObject.put("access_token", wechat.getDb().getToken());
            jsonObject.put("nickname", wechat.getDb().getUserName());
            jsonObject.put("headimgurl", wechat.getDb().getUserIcon());
            ByteArrayEntity entity = new ByteArrayEntity((jsonObject.toString().getBytes("utf-8")));
            Log.e(tag, jsonObject.toString() );
            PersistentCookieStore myCookieStore = new PersistentCookieStore(MainActivity.this);
            asyncHttpClient.setCookieStore(myCookieStore);
            asyncHttpClient.post(this, "http://uc.wuhan2019mwg.cn/?m=member&a=public_weixin_token_login", entity, "application/json", new AsyncHttpResponseHandler() {
                @Override
                public void onSuccess(String s) {
                    super.onSuccess(s);
                    Log.e(tag, "onSuccess " + s);
                    getCookieText();
                    //ToastUtils.showSingleToast(getApplicationContext(),s);

                    Intent intent = new Intent();
                    intent.putExtra(Constance.URL,"http://uc.wuhan2019mwg.cn/index.php?m=member&c=index&a=login");
                    intent.setClass(getApplicationContext(),OtherWebActivity.class);
                    startActivity(intent);
//                    mPlatform.removeAccount();
//                    mPlatform=null;
                    wechat.removeAccount();//removeAccount();
                    wechat.removeAccount(true);
                    wechat=null;
                }


                @Override
                public void onFailure(Throwable throwable, String s) {
                    super.onFailure(throwable, s);
                    Log.e(tag, "onFailure " + s);
                }

            });


        } catch (Exception e) {
            e.printStackTrace();
        }



    }




    private String getCookieText() {
        PersistentCookieStore myCookieStore = new PersistentCookieStore(MainActivity.this);
        List<Cookie> cookies = myCookieStore.getCookies();

        WuhanMwgApplication.setCookies(cookies);
        for (Cookie cookie : cookies) {
            //Log.d(tag, cookie.getName() + " = " + cookie.getValue());
        }
        StringBuffer sb = new StringBuffer();
        for (int i = 0; i < cookies.size(); i++) {
            Cookie cookie = cookies.get(i);
            String cookieName = cookie.getName();
            String cookieValue = cookie.getValue();
            if (!TextUtils.isEmpty(cookieName)
                    && !TextUtils.isEmpty(cookieValue)) {
                sb.append(cookieName + "=");
                sb.append(cookieValue + ";");
            }
        }
//        Log.e("cookie ", sb.toString());
        WuhanMwgApplication.strcookie = sb.toString();
        return sb.toString();
    }


}
